﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCService.Sim.Company.Department;
using BCDto.Sim.Company.Department;
using BCTMSRESTful.Configs;

namespace BCTMSRESTful.Controllers.Management
{
    /// <summary>
    /// 企业部门管理
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class DepartmentController : ManagementBaseController
    {
        #region 私有变量

        private readonly IDepartmentService departmentService;

        #endregion 私有变量

        #region 构造函数

        public DepartmentController(IDepartmentService departmentService)
        {
            this.departmentService = departmentService;
        }

        #endregion 构造函数

        #region 公共方法
        #region 创建部门信息
        /// <summary>
        /// 创建部门信息
        /// </summary>
        /// <response code="200">提交车辆审核成功。</response>
        /// <response code="401">用户无权限访问。</response>
        [HttpPost]
        [Log("创建部门信息")]
        [ProducesResponseType(typeof(DepartmentDto), 201)]
        public IActionResult Post([FromBody]DepartmentRequestDto dto)
        {
            return StatusCode((int)HttpStatusCode.Created,departmentService.Add(this.Admin,dto));
        }
        #endregion

        #region 修改部门信息
        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <response code="201">数据修改成功。</response>
        /// <response code="401">用户无权限访问。</response>
        [HttpPut("{department_id}")]
        [Log("修改部门信息")]
        [ProducesResponseType(typeof(DepartmentDto), 200)]
        public IActionResult Put([FromRoute(Name = "department_id")]int departmentId, [FromBody]DepartmentRequestDto dto)
        {
            var updateStatus = departmentService.Update(departmentId, this.Admin, dto);
            return StatusCode((int)HttpStatusCode.OK, updateStatus);
        }
        #endregion

        #region 删除部门信息
        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <param name="departmentId">部门id</param>
        /// <response code="204">数据删除成功。</response>
        /// <response code="401">用户无权限访问。</response>
        [HttpDelete("{department_id}")]
        [Log("删除部门信息")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "department_id")]int departmentId)
        {
            var deleteStatus = departmentService.Delete(departmentId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取公司根级部门列表
        /// <summary>
        /// 获取公司根级部门列表
        /// </summary>
        /// <response code="200">获取公司下根级部门列表</response>
        /// <returns></returns>
        [HttpGet("[action]/{company_id}")]
        [ProducesResponseType(typeof(IEnumerable<DepartmentDto>), 200)]
        public IActionResult QueryRootDepartment([FromRoute(Name = "company_id")]int companyId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.departmentService.Root(companyId));
        }
        #endregion

        #region 获取子级部门列表
        /// <summary>
        /// 获取子级部门列表
        /// </summary>
        /// <param name="departmentId">部门ID</param>
        /// <response code="200">获取子级部门列表</response>
        /// <returns></returns>
        [HttpGet("[action]/{department_id}")]
        [ProducesResponseType(typeof(IEnumerable<DepartmentDto>), 200)]
        public IActionResult QueryChildren([FromRoute(Name = "department_id")]int departmentId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.departmentService.Children(departmentId));
        }
        #endregion

        #region 获取公司、部门及所有子级部门树型列表
        /// <summary>
        /// 获取公司、部门及所有子级部门树型列表
        /// </summary>
        /// <param name="levelType">级别类型</param>
        /// <param name="selectable">允许选中</param>
        /// <param name="allowAcrossCompany">允许跨公司</param>
        /// <response code="200">获取公司、部门及所有子级部门树型列表</response>
        /// <returns></returns>
        [HttpGet("[action]/{level_type}")]
        [ProducesResponseType(typeof(List<object>), 200)]
        public IActionResult CompanyDepartmentPersonTree([FromRoute(Name ="level_type")]string levelType, 
            [FromQuery(Name = "selectable")]bool selectable, 
            [FromQuery(Name = "allow_across_company")] bool allowAcrossCompany = true)
        {
            return StatusCode((int)HttpStatusCode.OK, this.departmentService.CompanyDepartmentPersonTree(levelType, selectable, allowAcrossCompany, this.Admin.CompanyId));
        }
        /// <summary>
        /// 获取公司、部门及所有子级部门树型列表
        /// </summary>
        /// <param name="levelType">级别类型</param>
        /// <param name="selectable">允许选中</param>
        /// <response code="200">获取公司、部门及所有子级部门树型列表</response>
        /// <returns></returns>
        [HttpGet("[action]/{level_type}")]
        [ProducesResponseType(typeof(List<object>), 200)]
        public IActionResult SingleCompanyDepartmentPersonTree([FromRoute(Name = "level_type")] string levelType,
            [FromQuery(Name = "selectable")] bool selectable)
        {
            return StatusCode((int)HttpStatusCode.OK, this.departmentService.CompanyDepartmentPersonTree(levelType, selectable, this.Admin.CompanyId, this.Admin.DepartmentId.Value));
        }
        #endregion

        #region 获取部门及所有子级部门树型列表
        /// <summary>
        /// 获取部门及所有子级部门树型列表
        /// </summary>
        /// <param name="companyId">公司Id</param>
        /// <param name="departmentId">部门Id</param>
        /// <response code="200">获取部门及所有子级部门树型列表</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(List<object>), 200)]
        public IActionResult QueryChildrenTree([FromQuery(Name = "company_id")]int companyId, [FromQuery(Name = "department_id")]int? departmentId)
        {
            if (this.Admin.IsSystemAccount)
            {
                if (departmentId.HasValue)
                {
                    return StatusCode((int)HttpStatusCode.OK,this.departmentService.ChildrenTree(companyId, departmentId));
                }
                else
                {
                    return StatusCode((int)HttpStatusCode.OK,this.departmentService.ChildrenTree(companyId, null));
                }
            }
            else
            {

                if (departmentId.HasValue)
                {
                    return StatusCode((int)HttpStatusCode.OK,this.departmentService.ChildrenTree(this.Admin.CompanyId, departmentId));
                }
                else
                {
                    return StatusCode((int)HttpStatusCode.OK,this.departmentService.ChildrenTree(this.Admin.CompanyId, this.Admin.DepartmentId));
                }
            }
        }
        #endregion

        #region 获取部门列表分页
        /// <summary>
        /// 获取部门列表分页
        /// </summary>
        /// <param name="companyId">公司Id</param>
        /// <param name="departmentName">企业部门名称</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="pageIndex">起始页</param>
        /// <param name="pageSize">页数量</param>
        /// <response code="200">数据查询成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<DepartmentDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "company_id")] int? companyId,
            [FromQuery(Name = "department_name")]string departmentName,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int pageIndex = 1,
            [FromQuery(Name = "size")]int pageSize = 10)
        {
            var data = departmentService.Query(companyId.HasValue? companyId.Value:this.Admin.CompanyId, departmentName, start, end, sortName, ascending, pageIndex, pageSize);
            return StatusCode((int)HttpStatusCode.OK,data);
        }
        #endregion

        #region 获取公司下所有部门列表
        /// <summary>
        /// 获取公司下所有部门列表
        /// </summary>
        /// <param name="companyId">公司Id</param>
        /// <response code="200">数据查询成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpGet("company/{company_id}")]
        [ProducesResponseType(typeof(IEnumerable<DepartmentDto>), 200)]
        public IActionResult GetList([FromRoute(Name = "company_id")]int companyId)
        {
            var data = departmentService.GetList(companyId);
            return StatusCode((int)HttpStatusCode.OK,data);
        }
        #endregion

        #region 获取部门信息
        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <param name="departmentId">部门id</param>
        /// <response code="200">数据查询成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpGet("{department_id}")]
        [ProducesResponseType(typeof(DepartmentDto), 200)]
        public IActionResult Get([FromRoute(Name = "department_id")]int departmentId)
        {
            var data = departmentService.Get(departmentId);
            return StatusCode((int)HttpStatusCode.OK,data);
        }
        #endregion

        #region 部门员工人列表
        /// <summary>
        /// 部门员工人列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<DepartmentPrincipalDto>), 200)]
        public IActionResult StaffList([FromQuery(Name = "department_id")]int? departmentId)
        {
            var data = departmentService.StaffList(this.Admin.CompanyId, departmentId);
            return StatusCode((int)HttpStatusCode.OK,data);
        }
        #endregion

        #region 设置部门负责人信息
        /// <summary>
        /// 设置部门负责人信息
        /// </summary>
        /// <response code="204">设置成功。</response>
        /// <response code="401">用户无权限访问。</response>
        [HttpPut("SetPrincipalInfo/{department_id}")]
        [Log("设置部门负责人信息")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult SetPrincipalInfo([FromRoute(Name = "department_id")] int departmentId, [FromBody] DepartmentPrincipalPutDto dto)
        {
            dto.DepartmentId = departmentId;
            departmentService.SetPrincipalInfo(dto);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 移除部门负责人信息
        /// <summary>
        /// 移除部门负责人信息
        /// </summary>
        /// <response code="201">数据修改成功。</response>
        /// <response code="204">无内容。</response>
        /// <response code="401">用户无权限访问。</response>
        [HttpPut("RemovePrincipalInfo/{department_id}")]
        [Log("移除部门负责人信息")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult RemovePrincipalInfo([FromRoute(Name = "department_id")]int departmentId, [FromBody]DepartmentPrincipalPutDto dto)
        {
            dto.DepartmentId = departmentId;
            departmentService.RemovePrincipalInfo(this.Admin,dto);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion
        #endregion 公共方法
    }
}
